// pages/teamListLeaderPage/teamListLeaderPage.js
import {
  request
} from '../../request/index.js';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
      id: 0,
      value: "所有团队",
      isActive: true
    }],
    teamList: [],
    defaultTeamId: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let res = await request({
      url: '/team/getAllTeams'
    })
    this.setData({
      teamList: res.data.data
    })
    if (app.globalData.defaultTeamId) {
      this.setData({
        defaultTeamId: app.globalData.defaultTeamId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.defaultTeamId) {
      request({
        url: '/map/getMapListByTeamId/' + this.data.defaultTeamId
      }).then(res => {
        app.globalData.mapGlobal = res.data.data
      })
    }
    app.globalData.teamListPageHideFlag = true
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  joinTeamSet(e) {
    this.setData({
      selectedTeamId: e.currentTarget.dataset.teamid
    })
    // 显示遮罩层
    var animation2 = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation2
    animation2.translateY(300).step()
    this.setData({
      animationData2: animation2.export(),
      showModalStatus2: true
    })
    setTimeout(function () {
      animation2.translateY(0).step()
      this.setData({
        animationData2: animation2.export()
      })
    }.bind(this), 200)
  },
  hideModal2() {
    // 隐藏遮罩层
    var animation2 = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation2 = animation2
    animation2.translateY(300).step()
    this.setData({
      animationData2: animation2.export(),
    })
    setTimeout(function () {
      animation2.translateY(0).step()
      this.setData({
        animationData2: animation2.export(),
        showModalStatus2: false
      })
    }.bind(this), 200)
  },
  async team_moren(e) {
    // 判断是否是点击的已激活的地图
    if(this.data.defaultTeamId != this.data.selectedTeamId && app.globalData.activeMapIdGlobal){
      app.globalData.activeMapIdGlobal = ''
    }
    this.setData({
      defaultTeamId: this.data.selectedTeamId
    })
    app.globalData.defaultTeamId = this.data.defaultTeamId
    // 发起请求
    let res = await request({
      url: '/teamuser/updateDefaultTeam?teamId=' + app.globalData.defaultTeamId + '&userId=' + app.globalData.userIdGlobal,
      method: 'PUT'
    })
    this.hideModal2()
  }
})